<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Model\Admin\Product;
use App\Model\Admin\ProductAttr;
use App\Model\Front\Category;
use App\Model\Front\ShoppingCart;
use Illuminate\Http\Request;

class DetailsController extends Controller
{


	public function details(Product $product, Category $category)
    {
    	
        RecommendController::create($product->pro_code, auth()->id());
        $wishlist = $product->wishlist()->get();
        // $menuBar = $product->category; 
        // $parent = Category::where('id',$menuBar->parent_id)->get(); 
        $menu = $category->where('id',$product->cate_id)->first();
        // $info = $category->parenPath($menu->id);

        return view('front.details', compact('product'))->withWcount($wishlist);
    }

	/**
	 * [show_attr ajax请求当前属性]
	 * @param  Request $request [请求注入]
	 * @return [type]           [description]
	 */
    public function show_attr(Request $request)
    {
    	$id = $request->id;
    	//取出单选的属性
    	$oneSelect = ProductAttr::rightJoin('product_attributes','product_attrs.attr_id','product_attributes.id')
    							  ->select('product_attributes.attr_name', 'product_attributes.attr_type','product_attrs.*')
    							  ->where('product_attributes.attr_type', '=', 1)
    							  ->where('product_id',$id)
    							  ->get();
		$result = array();
		$az = $oneSelect->toArray() ;
		foreach ($az as $key=>$value) {
			$result[$value['attr_name']][] = $value;
		}
		return $result;
		 
    }

    public function unique_attr(Request $request)
    {
        $id = $request->id;
        //取出单选的属性
        $oneSelect = ProductAttr::rightJoin('product_attributes','product_attrs.attr_id','product_attributes.id')
                                  ->select('product_attributes.attr_name', 'product_attributes.attr_type','product_attrs.*')
                                  ->where('product_attributes.attr_type', '=', 0)
                                  ->where('product_id',$id)
                                  ->get();
        return $oneSelect;
    }
}
